package www.houdoudou.com.supplier;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonMathFunction;
import www.houdoudou.com.common.ConnFactory;
import www.houdoudou.com.common.TelnumVerifyManagement;

public class OrderProductManagement {

	
	
	 public static JSONObject  manage(String jsonData){
	  	   
	   	  JSONObject result = new JSONObject();
	   		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
	   		//判断请求的功能d
	   		int FunctionCode=jsonfrom.getInt("FunctionCode");
	   		   PreparedStatement pstm=null; 
	   		   Statement  stm=null;
	   		   ResultSet rs=null; 	
	   		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
	   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
	   		   switch(FunctionCode){
	   		
	   		   //删除
	   		   case 3:{
	   			   try{
	   			   JSONArray Products= jsonfrom.getJSONArray("Products");
	   			   
	   			   for(int i=0 ;i<Products.size();i++){
	   				   
	   			   }
	   			  
	   			   }catch(Exception e){
	   				   e.printStackTrace();
	   				result.put("ActionStatus", false);
					result.put("ErrorInfo", "删除失败");
	   			   }
	   		     break; }	   		   
	   		   
	   		   //新增
	   		  case 4:{ 
	   			  
	   			        
	   			             
	   			  
	   			  
				   try {		String sql1="insert  into Tb_Order ";
				                String sql2=" (Order_State,Order_Secret,Order_Money,Box_Num,Product_Num,Customer_Telnum,Machine_ID,"
				                		+ "Machine_Address,Machine_Name,Supplier_Telnum,Supplier_Name,Service_Telnum,Arrive_DateTime,"
				                		+ "Placed_DateTime,Arrived_DateTime,Taked_DateTime,Refused_DateTime,Arrive_EarliestTime,"
				                		+ "Take_LatestTime,Retrieve_LatestTime,Refund_ID) ";
				                String sql3=" (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
				        	// String sql = "insert  into Tb_Order (Customer_Telnum,Customer_Secret) values (?,?)";
				                String sql=sql1+sql2+" values "+sql3;
				                pstm=conn.prepareStatement(sql);
								pstm.setString(1, jsonfrom.getString("Order_State"));
								pstm.setString(2, "");	
								pstm.setDouble(3, jsonfrom.getDouble("Order_Money"));
								pstm.setInt(4, jsonfrom.getInt("Box_Num"));	
								pstm.setInt(5, jsonfrom.getInt("Product_Num"));
								pstm.setString(6, jsonfrom.getString("Customer_Telnum"));	
								pstm.setInt(7, jsonfrom.getInt("Machine_ID"));
							
							    pstm.executeUpdate();						    
							   
							    
							    rs=pstm.getGeneratedKeys();
								if(rs.next()){
									int Product_ID= rs.getInt(1);
								result.put("Product_ID",Product_ID);
								}
								 result.put("ActionStatus", true);
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "查询失败");
					}finally{
						ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
					}
				   
			                 break;} //end of case 4
	   		  
	   		   }// end of switch functionCode
	   		   
	   		   
	   		   return result; 
	   		   
	 }
}
